﻿@using hc.epm.Common;
@using hc.Plat.Common.Extend;
@{
    ViewBag.Title = "新增危险作业";
}
<div class="contentData">
    <form id="mainForm">
        <div class="tab">
            <div class="long-text">
                <label for="ProjectName"><span class="star">*</span>&nbsp;项目名称&nbsp;:</label>
                <input type="text" name="ProjectName" id="ProjectName" autocomplete="off" readonly value="@ViewBag.ProjectName" />
                <input type="hidden" name="ProjectId" id="ProjectId" value="@ViewBag.ProjectId" />
                <i class="pic iconfont" id="ProjectName_icon" width="20" height="30" style="cursor:pointer; left:596px;">&#xe76a;</i>
            </div>
            @*<div class="long-text">
                    <label for="TaskName"><span class="star">*</span>&nbsp;作业名称&nbsp;:</label>
                    <input type="text" name="TaskName" id="TaskName" value="" autocomplete="off" />
                </div>*@
            <div class="short-text">
                <label><span class="star">*</span>&nbsp;作业时间&nbsp;:</label>
                <input type="text" id="StartTime" name="StartTime" autocomplete="off" isShowToday="false" onClick="WdatePicker({ el: 'StartTime',isShowToday:false})" onfocus="WdatePicker({minDate:'%y-%M-#{%d+1}',isShowToday:false})">
                <i onClick="WdatePicker({ el: 'StartTime', isShowToday: false })" class="pic iconfont" width="20" height="30" style="cursor:pointer">&#xe7e2;</i>
            </div>
            @*<div class="short-text">
                    <label><span class="star">*</span>&nbsp;结束时间&nbsp;:</label>
                    <input type="text" id="EndTime" name="EndTime" autocomplete="off" onClick="WdatePicker({ el: 'EndTime' })" onfocus="WdatePicker({minDate:'%y-%M-#{%d+1}'})">
                    <i onClick="WdatePicker({ el: 'EndTime' })" class="pic iconfont" width="20" height="30" style="cursor:pointer">&#xe7e2;</i>
                </div>*@
            <div class="short-text">
                <label><span class="star">*</span>&nbsp;作业分类&nbsp;:</label>
                @Html.DropDownList("WorkType", null, new { @class = "form-control selectpicker  show-tick" })
                <input type="hidden" name="TaskTypeNo" id="TaskTypeNo" />
                <input type="hidden" name="TaskTypeName" id="TaskTypeName" />
            </div>
            <div class="intro">
                <div><label for="TaskContent"><span class="star">*</span>&nbsp;作业内容&nbsp;:</label></div>
                <div>
                    <textarea class="sub-description" id="TaskContent" name="TaskContent" style="width:77%;"></textarea>
                </div>
            </div>
            @*<div class="long-text" style="margin-top:30px;">
                    <label for="TaskArea"><span class="star"></span>&nbsp;作业区域&nbsp;:</label>
                    <input type="text" name="TaskArea" id="TaskArea" autocomplete="off" />
                </div>*@
            <div class="long-text">
                <label><span class="star"></span>&nbsp;施工单位&nbsp;:</label>
                <input type="text" name="workCompanyName" id="workCompanyName" value="@ViewBag.WorkCompanyName" />
                <input type="hidden" name="WorkCompanyId" id="WorkCompanyId" value="@ViewBag.WorkCompanyId" />
                <i class="pic iconfont" id="workCompanyName_icon" width="20" height="30" style="cursor:pointer; left:596px;">&#xe76a;</i>
            </div>
            @*<div class="long-text">
                    <label><span class="star"></span>&nbsp;防护措施&nbsp;:</label>
                    <input type="text" name="Protective" id="Protective" autocomplete="off" />
                </div>*@
            <div class="upload" style="margin-bottom:0px;">
                <div><label><span class="star">*</span>&nbsp;附件上传&nbsp;:</label></div>
                <div class="btn-upload">
                    <input type="button" id="btnUploadFile" style="position: relative; z-index: 1;" value="上传附件" />
                    <input type="file" id="loadFile" name="loadFile" style="display:none" onchange="uploadFile()">
                    <span style="vertical-align:bottom; margin-left:15px; color:#B7B7B7;"></span>
                </div>
            </div>
            <div style="width:100%;" id="fileListFile">
                <ul class="fileShow"></ul>
                <input type="hidden" name="fileDataJsonFile" id="fileDataJsonFile">
            </div>
            <div class="button-group btn-center">
                <button type="button" class="btnSub" data-state="@(ApprovalState.WaitAppr.GetValue().ToString())">提交</button>
                <button type="button" class="btnSub" data-state="@(ApprovalState.Enabled.GetValue().ToString())">保存</button>
            </div>
            <input type="hidden" name="State" id="State" />
            <input type="hidden" name="EndTime" id="EndTime" />
        </div>
    </form>
</div>
<script>
    //判断开始时间和结束时间
    //$("#StartTime,#EndTime").blur(function () {
    //    var strStartTime = $("#StartTime").val();
    //    var endTime = $("#EndTime").val();
    //    var startNum = parseInt(strStartTime.replace(/-/g, ''), 10);
    //    var endNum = parseInt(endTime.replace(/-/g, ''), 10);
    //    if (startNum > endNum) {
    //        layer.alert("结束时间不能小于开始时间!", { icon: 2 });
    //        $("#EndTime").val("");
    //        return false;
    //    }
    //});

    var fileDataArray = [];
    function uploadFile() {
        if (document.getElementById("loadFile").files.length == 0) {
            return;
        }
        var fileObj = document.getElementById("loadFile").files[0]; // js 获取文件对象
        var filename = fileObj.name;
        //上传附件不能含有特殊字符
        var regEn = /[`~!@{@Html.Raw("@");}#$%^&*()_+<>?:"{},\/;'[\]]/im,
            regCn = /[！#￥（——）：；“”‘、，|《。》？、【】[\]]/im;

        if (regEn.test(filename) || regCn.test(filename)) {
            layer.alert("附件名称不能包含特殊字符", { icon: 2 });
            return false;
        }
        var formFile = new FormData();
        formFile.append("file", fileObj);
        var path = formFile;

        $.ajax({
            url: "/Upload/UploadHB",
            type: "POST",
            data: path,
            contentType: false,
            processData: false,
            success: function (data) {
                for (var i = 0; i < data.length; i++) {
                    var obj = {};
                    obj.Name = data[i].Name;
                    obj.GuidId = data[i].GuidId;
                    obj.Size = data[i].Size;
                    obj.ImageType = data[i].ImageType;
                    obj.Group = data[i].ResponseObject.FDFS_GROUP;
                    obj.Url = data[i].ResponseObject.FDFS_NAME;
                    fileDataArray.push(obj);

                    if (data[i].ImageType == null) {
                        var imgUrl = getFileImg(obj.Name);
                        var odiv = '<li id="' + obj.GuidId + '">';
                        odiv += '<div><img src="' + imgUrl + '" style="width: 20px;height: 20px;cursor: pointer;" class="imgShow"/><a href="javascript:;" target="_blank">' + obj.Name + '</a></div>';
                        odiv += '<div style="text-align:right; width:80px;">' + obj.Size + '</div>';
                        odiv += '<span style="margin-left:20px;">' + data[i].UploadName + '</span>';
                        odiv += '<span style="margin-left:20px;">' + new Date(formatDateByJson(data[i].UploadDate)).Format("yyyy-MM-dd") + '</span>';
                        odiv += '<span style="margin-left:20px;cursor: pointer;color:blue" class="fileDel" data-val="' + obj.GuidId + '">删除</span>'
                        odiv += '</li>';
                        $('#fileListFile .fileShow').append(odiv);
                    }
                }
                fileDataJson = JSON.stringify(fileDataArray);
                $("#fileDataJsonFile").val(fileDataJson);
                $("#loadFile").val("");
            }
        });
    }

    $(function () {
        $('#btnUploadFile').click(function () {
            document.getElementById("loadFile").click();
        });
        //表单验证
        var validator = $("#mainForm").validate({
            errorClass: "validaterror",
            validClass: "validatevalid",
            errorElement: "span",
            success: function (label) {
                label.addClass("validatevalid").html("&nbsp;");
            },
            rules: {
                ProjectName: {
                    required: true,
                    maxlength: 200
                },
                //TaskName: {
                //    required: true,
                //    maxlength: 200
                //},

                StartTime: {
                    required: true,
                    date: true
                },
                //EndTime: {
                //    required: true,
                //    date: true
                //},

                TaskContent: {
                    required: true,
                    maxlength: 500
                },
                //TaskArea:{
                //    maxlength: 254
                //},
                WorkType: {
                    required: true,
                }
            },
            messages: {
                ProjectName: {
                    required: "项目名称不能为空",
                    maxlength: "项目名称不能超过200字符"
                },
                //TaskName: {
                //    required: "作业名称不能为空",
                //    maxlength: "作业名称不能大于200字符"
                //},

                StartTime: {
                    required: "开始时间不能为空",
                    date: "日期格式错误"
                },
                //EndTime: {
                //    required: "结束时间不能为空",
                //    maxlength: "日期格式错误",
                //},

                TaskContent: {
                    required: "作业内容不能为空",
                    maxlength: "作业内容不能超过500字符"
                },
                //TaskArea: {
                //    maxlength: "作业区域不能超过254字符"
                //},
                WorkType: {
                    required: "作业分类不能为空",
                }
            }

        })
        layui.use(['layer', 'form', 'element', 'laypage'], function () {
            var layer = layui.layer, form = layui.form, element = layui.element, $ = layui.jquery, laypage = layui.laypage;
            //选择项目
            $("#ProjectName,#ProjectName_icon").click(function () {
                layer.open({
                    type: 2,
                    title: GetLayerTitle("选择项目"),
                    shadeClose: false, //点击遮罩关闭层
                    area: ['800px', '600px'],
                    content: '/Comm/SelectProjectAll',
                    btn: ["确定", "取消"],
                    yes: function (index, layero) {
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        var obj = iframeWin.getSelectData();
                        if (obj && obj.flag && obj.data) {
                            $("#ProjectName").val(obj.data[0].Name);
                            $("#ProjectId").val(obj.data[0].Id);
                            layer.close(index);
                            //alert("id:" + obj.data[0].id + " name:" + obj.data[0].name);
                        } else if (!obj.flag) {
                            parent.layer.msg(obj.msg, { time: 1000, icon: 2 });
                            return false;
                        }
                    },
                    btn2: function (index, layero) {
                    },
                    cancel: function (index, layero) {
                    },
                });
            });
            $('#WorkType').change(function () {
                var TaskTypeNo = $('#WorkType').val();
                var TaskTypeName = $('#WorkType option:selected').text();
                $("#TaskTypeNo").val(TaskTypeNo)
                $("#TaskTypeName").val(TaskTypeName)

            })

            //删除附件
            $("#fileListFile").on("click", ".fileDel", function () {
                $(this).parents("li").remove();
                var toremove = '';
                var id = $(this).data("val");
                //----删除初始文件对应的值
                fileDataArray = fileDataArray.filter(function (item, index, arr) {
                    return item.GuidId != id
                })
                fileDataJsonFile = JSON.stringify(fileDataArray);
                $("#fileDataJsonFile").val(fileDataJsonFile);
            });
            //提交数据
            $(".btnSub").click(function btnSub() {
                $("#EndTime").val($("#StartTime").val());
                var state = $(this).data("state");
                $("#State").val(state)
                if ($(".fileShow li").length == 0) {
                    layer.alert("请先上传附件", { icon: 2 });
                    return false;
                }
                if (!validator.form()) {
                    return;
                }

                var data = $("#mainForm").serialize();
                data = decodeURIComponent(data, true)
                $.ajax({
                    url: "/Work/Add",
                    type: "post",
                    data: data,
                    success: function (data) {
                        if (data.Flag) {
                            layer.msg("操作成功", { time: 1000, icon: 1 }, function () {
                                window.location.href = "/Work/Index";
                            });
                        } else {
                            layer.alert(data.Message, { icon: 2 });
                        }
                    }
                });
            });
        });
        //选择施工单位
        //$(".WorkCompanyName").click(function () {
        //    var projectName = $("#ProjectName").val();
        //    var projectId = $("#ProjectId").val();
        //    if (projectId == "" || projectName == "") {
        //        parent.layer.msg("请先选择项目", { time: 1000, icon: 2 });
        //        return false;
        //    }
        //    layer.open({
        //        type: 2,
        //        title: GetLayerTitle("选择施工单位"),
        //        shadeClose: false, //点击遮罩关闭层
        //        area: ['800px', '600px'],
        //        content: '/Comm/SelectProjectCustomer?selectType=1&projectId=' + projectId + "&companyType=土建,包装,安装",
        //        btn: ["确定", "取消"],
        //        yes: function (index, layero) {
        //            var iframeWin = window[layero.find('iframe')[0]['name']];
        //            var obj = iframeWin.getSelectData();
        //            if (obj && obj.flag && obj.data) {
        //                $("#workCompanyName").val(obj.data[0].Name);
        //                $("#WorkCompanyId").val(obj.data[0].Id);
        //                layer.close(index);
        //            } else if (!obj.flag) {
        //                parent.layer.msg(obj.msg, { time: 1000, icon: 2 });
        //                return false;
        //            }
        //        },
        //        btn2: function (index, layero) {
        //            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
        //            parent.layer.close(index);//关闭窗口
        //        },
        //        cancel: function (index, layero) {
        //        },
        //    });
        //});

        $("#workCompanyName").click(function () {
            var _this = $(this);
            layer.open({
                type: 2,
                title: GetLayerTitle("选择验收单位"),
                shadeClose: false, //点击遮罩关闭层
                area: ['800px', '600px'],
                skin: 'frame_button',
                btnAlign: 'c',
                content: '/Comm/SelectCustomerByNameOrType',
                btn: ["确定", "取消"],
                yes: function (index, layero) {
                    var iframeWin = window[layero.find('iframe')[0]['name']];
                    var obj = frames['layui-layer-iframe' + index].getSelectData();
                    if (obj.flag === false) {
                        layer.alert(obj.msg, { icon: 2 });
                    } else {
                        $("#workCompanyName").val(obj.data[0].Name);
                        $("#WorkCompanyId").val(obj.data[0].Id);
                    }
                    layer.close(index);
                },
                btn2: function (index, layero) {
                    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);//关闭窗口
                },
                cancel: function (index, layero) {
                },
            });
        });
    });
</script>
